package com.github.slprj.slake.log;

import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * Des:
 *
 * @author captain
 * @date 2022/8/19 16:36
 */
public class JdkLog implements SlLog {

    private final Logger log;

    public JdkLog(Logger log) {
        this.log = log;
    }

    public void log(Level level, FormattingTuple ft) {
        LogRecord lr = new LogRecord(level, ft.getMessage());
        lr.setLoggerName(log.getName());
        lr.setThrown(ft.getThrowable());
        log.log(lr);
    }

    @Override
    public boolean isDebugEnabled() {
        return log.isLoggable(Level.FINE);
    }

    @Override
    public void debug(String message) {
        if (isDebugEnabled()) {
            log.fine(message);
        }
    }

    @Override
    public void debug(String format, Object arg) {
        if (isDebugEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg);
            log(Level.FINE, ft);
        }

    }

    @Override
    public void debug(String format, Object arg1, Object arg2) {
        if (isDebugEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
            log(Level.FINE, ft);
        }
    }

    @Override
    public void debug(String format, Object... args) {
        if (isDebugEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, args);
            log(Level.FINE, ft);
        }
    }

    @Override
    public void debug(String message, Throwable t) {
        if (isDebugEnabled()) {
            log.log(Level.FINE, message, t);
        }
    }

    @Override
    public boolean isInfoEnabled() {
        return log.isLoggable(Level.WARNING);
    }

    @Override
    public void info(String message) {
        if (isInfoEnabled()) {
            log.info(message);
        }
    }

    @Override
    public void info(String format, Object arg) {
        if (isInfoEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg);
            log(Level.INFO, ft);
        }
    }

    @Override
    public void info(String format, Object arg1, Object arg2) {
        if (isInfoEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
            log(Level.INFO, ft);
        }
    }

    @Override
    public void info(String format, Object... args) {
        if (isInfoEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, args);
            log(Level.INFO, ft);
        }
    }

    @Override
    public void info(String message, Throwable t) {
        log.log(Level.INFO, message, t);
    }

    @Override
    public boolean isWarnEnabled() {
        return log.isLoggable(Level.WARNING);
    }

    @Override
    public void warn(String message) {
        if (isInfoEnabled()) {
            log.warning(message);
        }
    }

    @Override
    public void warn(String format, Object arg) {
        if (isWarnEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg);
            log(Level.WARNING, ft);
        }
    }

    @Override
    public void warn(String format, Object arg1, Object arg2) {
        if (isWarnEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
            log(Level.WARNING, ft);
        }
    }

    @Override
    public void warn(String format, Object... args) {
        if (isWarnEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, args);
            log(Level.WARNING, ft);
        }
    }

    @Override
    public void warn(String message, Throwable t) {
        if (isWarnEnabled()) {
            log.log(Level.WARNING, message, t);
        }
    }

    @Override
    public boolean isErrorEnabled() {
        return log.isLoggable(Level.SEVERE);
    }

    @Override
    public void error(String message) {
        if (isErrorEnabled()) {
            log.log(Level.SEVERE, message);
        }
    }

    @Override
    public void error(String format, Object arg) {
        if (isErrorEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg);
            log(Level.SEVERE, ft);
        }
    }

    @Override
    public void error(String format, Object arg1, Object arg2) {
        if (isErrorEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
            log(Level.SEVERE, ft);
        }
    }

    @Override
    public void error(String format, Object... args) {
        if (isErrorEnabled()) {
            FormattingTuple ft = MessageFormatter.format(format, args);
            log(Level.SEVERE, ft);
        }
    }

    @Override
    public void error(String message, Throwable t) {
        log.log(Level.SEVERE, message, t);
    }
}
